package com.adnaan.backend.common.serializer;

import com.adnaan.backend.common.annotation.FileUrl;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collection;

@Slf4j
@Component
public class FileUrlSerializer extends JsonSerializer<Object> implements ContextualSerializer, ApplicationContextAware {
    private static ApplicationContext applicationContext;
    private boolean addDomain;

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value == null) {
            gen.writeNull();
            return;
        }

        if (value instanceof String) {
            serializeString((String) value, gen);
        } else if (value instanceof Collection<?>) {
            gen.writeStartArray();
            for (Object item : (Collection<?>) value) {
                if (item instanceof String) {
                    serializeString((String) item, gen);
                }
            }
            gen.writeEndArray();
        }
    }

    private void serializeString(String url, JsonGenerator gen) throws IOException {
        if (url == null || url.isEmpty()) {
            gen.writeNull();
            return;
        }

        // 如果不需要域名，返回文件名
        if (!addDomain) {
            String fileName = extractFileName(url);
            gen.writeString(fileName);
            return;
        }

        // 如果需要域名，直接返回完整URL
        gen.writeString(url);
    }

    /**
     * 从URL中提取文件名
     */
    private String extractFileName(String url) {
        if (url == null || url.isEmpty()) {
            return url;
        }

        // 如果是完整URL，获取最后一个斜杠后的内容
        int lastSlashIndex = url.lastIndexOf('/');
        if (lastSlashIndex != -1 && lastSlashIndex < url.length() - 1) {
            return url.substring(lastSlashIndex + 1);
        }

        return url;
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        if (property != null) {
            FileUrl annotation = property.getAnnotation(FileUrl.class);
            if (annotation != null) {
                FileUrlSerializer serializer = new FileUrlSerializer();
                serializer.addDomain = annotation.addDomain();
                return serializer;
            }
            return prov.findValueSerializer(property.getType(), property);
        }
        return prov.findNullValueSerializer(null);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        FileUrlSerializer.applicationContext = applicationContext;
    }
} 